<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Models\School;
use App\Models\WorkTicket;
use App\Enums\WorkTicketStatusEnum;
use App\Admin\Repositories\WorkUser;
use App\Admin\Renderable\SchoolTable;
use Dcat\Admin\Http\Controllers\AdminController;

/**
 * @mixin \App\Models\WorkUser
 */
class WorkUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WorkUser(['schools', 'tickets']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('real_name', '姓名');
            $grid->column('username', '用户名');
            $grid->column('schools', '所属学校')->pluck('name')->label();
            $grid->column('tel', '电话');
            $grid->column('enable', '状态')->bool();
            $grid->column('memo', '备注');
            $grid->column('tickets', '工单数量')->display(function () {
                $unWorkCount = $this->tickets->whereIn('status', [WorkTicketStatusEnum::待处理, WorkTicketStatusEnum::处理中])->count();
                $workCount = $this->tickets->where('status', WorkTicketStatusEnum::已处理)->count();
                return '待处理:' . $unWorkCount . '<br/>已处理:' . $workCount;
            });
            $grid->column('created_at')->format('Y-m-d H:i:s')->sortable();
            $grid->column('updated_at')->format('Y-m-d H:i:s')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(2);
                $filter->like('real_name', '姓名')->width(3);
                $filter->like('username', '用户名')->width(3);
                $filter->like('tel', '电话')->width(3);
                $filter->equal('enable', '状态')->select([1 => '可用', 0 => '禁用'])->width(2);
                $filter->like('memo', '备注')->width(3);
                $filter->in('schools.id', '所属学校')->multipleSelectTable(SchoolTable::make())->width(4);
                $filter->between('created_at')->date()->width(4);
                $filter->between('updated_at')->date()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WorkUser(['schools', 'tickets']), function (Show $show) {
            $show->field('id');
            $show->field('real_name', '姓名');
            $show->field('tel', '电话');
            $show->field('enable', '状态')->bool();
            $show->field('memo', '备注');
            $show->field('created_at');
            $show->field('updated_at');

            $show->relation('schools', '所属学校', function ($model) {
                $grid = new Grid(new School);
                $grid->model()->whereHas('workUsers', function ($query) use ($model) {
                    $query->where('work_user_id', $model->id);
                });
                $grid->setResource('schools');
                $grid->id();
                $grid->column('name', '学校名称');
                $grid->column('address', '地址');
                $grid->column('tel', '电话');
                $grid->column('memo', '备注');
                $grid->column('created_at')->format('Y-m-d H:i:s')->sortable();
                $grid->column('updated_at')->format('Y-m-d H:i:s')->sortable();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->panel();
                    $filter->equal('id')->width(2);
                    $filter->like('name', '学校名称')->width(3);
                    $filter->like('address', '地址')->width(3);
                    $filter->like('tel', '电话')->width(3);
                    $filter->like('memo', '备注')->width(3);
                });
                return $grid;
            });

            $show->relation('tickets', '工单列表', function ($model) {
                $grid = new Grid(new WorkTicket);
                $grid->model()->where('work_user_id', $model->id);
                $grid->setResource('work-tickets');
                $grid->id();
                $grid->column('code', '工单号');
                $grid->column('school.name', '学校');
                $grid->column('room_number', '房间号');
                $grid->column('contact_name', '联系人');
                $grid->column('contact_tel', '联系电话');
                $grid->column('content', '内容');
                $grid->column('status')->using(WorkTicketStatusEnum::asSelectArray());
                $grid->column('finish_at')->format('Y-m-d H:i:s')->sortable();
                $grid->column('created_at')->format('Y-m-d H:i:s')->sortable();
                $grid->column('updated_at')->format('Y-m-d H:i:s')->sortable();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->panel();
                    $filter->like('code', '工单号')->width(2);
                    $filter->like('school.name', '学校')->width(2);
                    $filter->like('room_number', '房间号')->width(2);
                    $filter->like('contact_name', '联系人')->width(2);
                    $filter->like('contact_tel', '联系电话')->width(2);
                    $filter->like('content', '内容')->width(2);
                    $filter->in('status')->multipleSelect(WorkTicketStatusEnum::asSelectArray())->width(4);
                    $filter->between('finish_at')->date()->width(4);
                });
                return $grid;
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WorkUser(['schools']), function (Form $form) {
            $form->display('id');
            $form->text('real_name', '姓名')->required();
            $form->text('username', '用户名')->required();
            $form->password('password_confirmation', '密码')->required();
            $form->password('password', '确认密码')->rules(['required', 'confirmed'], ['required' => '确认密码不能为空', 'confirmed' => '两次密码不一致']);
            $form->multipleSelectTable('schools', '所属学校')->title('选择学校')
                ->from(SchoolTable::make())
                ->customFormat(function ($v) {
                    return collect($v)->pluck('id')->toArray();
                })->options(function ($v) {
                    if (!$v) {
                        return [];
                    }

                    $arr = School::whereIn('id', $v)->pluck('name', 'id');
                    return $arr;
                })->required();
            $form->text('tel', '电话');
            $form->switch('enable', '状态')->default(1);
            $form->text('memo', '备注');

            $form->display('created_at');
            $form->display('updated_at');

            $form->ignore(['password_confirmation']);
        });
    }
}
